package com.qiji.video.web.fcontroller;


import com.qiji.video.bean.dto.UserDto;
import com.qiji.video.exception.ServiceException;
import com.qiji.video.service.IUserActionService;
import com.qiji.video.service.IVideoDetailService;
import com.qiji.video.util.JwtUtil;
import com.qiji.video.util.Result;
import com.qiji.video.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api( tags = "前台----用户操作")
@RestController
@RequestMapping("/auth/user/action")
public class UserActionController {
    @Autowired
    public IUserActionService userActionService;

    //添加收藏
    @GetMapping("/collectionVideo")
    @ApiOperation("收藏视频，后端自定义设置如果已收藏会取消收藏")
    public Result collectionVideo(HttpServletRequest request,Integer vid) throws ServiceException {

        return userActionService.collectionVideo(getUserId(request),vid);
    }
    //添加播放量
    @GetMapping("/addClicks")
    @ApiOperation("添加播放量")
    public Result addClicks(HttpServletRequest request,Integer vid){
        Integer userId = getUserId(request);
        return userActionService.addClicks(userId,vid);
    }

    //点赞视频
    @GetMapping("/likeVideo")
    @ApiOperation("点赞视频，如果已经点赞则会取消")
    public Result likeVideo(HttpServletRequest request, Integer vid){
        Integer id = getUserId(request);

        return userActionService.likeVideo(id,vid);
    }

    private Integer getUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer id = Integer.parseInt(JwtUtil.getUserId(token));
        return id;
    }

}
